package com.zhx.threadcoreknowledge.stopthread;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-09-29 08:52
 * 用途         ： 正确停止线程内方法没有wait和sleep方法
 */
public class RightStopThreadWithoutSleep implements Runnable{
    @Override
    public void run() {
        int num = 0;
        /**
         * 方法内自己判断当前方法是否被isInterrupted了
         */
        while (num <= Integer.MAX_VALUE /2  && !Thread.currentThread().isInterrupted()){
            if(num % 10000 == 0){
                System.out.println(num+"是10000的倍数");
            }
            num++;
        }

        System.out.println("任务结束了");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new RightStopThreadWithoutSleep());
        thread.start();
        Thread.sleep(1000);
        /**
         * 向线程发送 interrupt方法
         */
        thread.interrupt();
    }
}
